package org.steven.crm.core.service.systemuser;

import org.steven.crm.core.service.BaseService;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wuzw
 * @Date 2023/5/16 16:03
 * @Version 1.0
 */
public class SystemRoleMenuService extends BaseService {
    @Override
    protected String getTable() {
        return "systemRoleMenu";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public List<String> getRoleMenuIds(String roleId) throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("systemRoleId", roleId);
        List<Map<String, String>> datas = DBManager.query(getSession(), getTable(), k);

        List<String> roleMenuIds = datas.stream().map(e -> e.get("systemMenuId")).collect(Collectors.toList());
        return roleMenuIds;
    }

    public List<String> getRoleMenuIds(List<String> roleIds) throws Exception {
        if (roleIds == null || roleIds.size() == 0) {
            return new ArrayList<>();
        }
        String sql = "select * from " + getTable() + " where systemRoleId in (" + StringUtils.join(roleIds, ",") + ")";
        List<Map<String, String>> datas = DBManager.query(getSession(), sql, new ArrayList<>());

        List<String> roleMenuIds = datas.stream().map(e -> e.get("systemMenuId")).collect(Collectors.toList());
        return roleMenuIds;
    }

    public void batchSave(String supplierSystemRoleId, Collection<String> newMenuIds) throws Exception {
        List<String> curMenuIds = getRoleMenuIds(supplierSystemRoleId);

        Collection<String> keepMenuIds = CollectionUtils.intersection(newMenuIds, curMenuIds);
        Collection<String> deleteMenuIds = CollectionUtils.subtract(curMenuIds, keepMenuIds);
        Collection<String> addMenuIds = CollectionUtils.subtract(newMenuIds, keepMenuIds);

        if (deleteMenuIds.size() > 0) {
            String sql = "delete from " + getTable() + " where systemRoleId = ? and supplierMenuId in ("
                    + deleteMenuIds.stream().collect(Collectors.joining(",")) + ")";
            List<String> p = new ArrayList<>();
            p.add(supplierSystemRoleId);
            DBManager.update(getSession(), sql, p);
        }

        if (addMenuIds.size() > 0) {
            List<Map<String, String>> addDatas = new ArrayList<>();
            for (String addMenuId : addMenuIds) {
                Map<String, String> addData = new HashMap<>();
                addData.put("systemRoleId", supplierSystemRoleId);
                addData.put("systemMenuId", addMenuId);
                addDatas.add(addData);
            }
            DBManager.batchInsert(getSession(), getTable(), addDatas);
        }
    }
}
